package com.csht.service;

import java.util.ArrayList;
import java.util.List;
import javax.annotation.Resource;
import org.junit.Test;
import com.csht.service.CompanyService;
import com.csht.command.GetCompanyCommand;
import com.csht.command.ListCompanyCommand;
import com.csht.command.CompanyBatchCommand;
import com.csht.command.CompanyCommand;
import com.csht.command.CompanyQueryCommand;
import com.csht.vo.CompanyVo;
import org.springframework.test.annotation.DirtiesContext;
import org.springframework.test.annotation.Rollback;
import org.springframework.test.context.ActiveProfiles;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.AbstractJUnit4SpringContextTests;

import com.winit.common.query.Page;
import com.winit.common.query.Sort;
import com.winit.common.spi.context.CommandContext;
import com.winit.pms.spi.v2.common.PageVo;

/**
 * 运输企业接口测试类
 * 
 * @version
 * 
 * <pre>
 * Author	Version		Date		Changes
 * houzw    1.0  2018年01月05日 Created
 * </pre>
 * 
 * @since 1.
 */
@DirtiesContext
@ActiveProfiles("test")
@ContextConfiguration(locations = { "/applicationContext.xml" })
public class CompanyServiceTest extends AbstractJUnit4SpringContextTests {

    @Resource
    private CompanyService companyService;
    
    /**
     * 虚拟上下文
     * 
     * @return
     */
    private CommandContext createContext() {
        CommandContext context = new CommandContext();
        context.setUsername("system");
        context.setOrgId(10000L);
        context.setLanguage("en_US");
        CommandContext.setContext(context);
        return context;
    }

    @Test
    @Rollback(false)
    public void testAdd() {
        CompanyCommand command = new CompanyCommand();
        command.setCtx(this.createContext());
        CompanyVo vo = new CompanyVo();
        command.setVo(vo);
        Long id = companyService.createCompany(command);
        System.out.println("id:" + id);
    }

    @Test
    @Rollback(false)
    public void testAddBatch() {
        CompanyBatchCommand command = new CompanyBatchCommand();
        command.setCtx(this.createContext());
        CompanyVo v1 = new CompanyVo();
        CompanyVo v2 = new CompanyVo();
        List<CompanyVo> list = new ArrayList<CompanyVo>();
        list.add(v1);
        list.add(v2);
        command.setVos(list);

        companyService.createBatchCompany(command);
    }

    @Test
    @Rollback(false)
    public void testDelete() {
        GetCompanyCommand command = new GetCompanyCommand();
        command.setCtx(this.createContext());
        command.setId(101L);
        companyService.deleteCompany(command);
    }

    @Test
    @Rollback(false)
    public void testDeleteBatch() {
        ListCompanyCommand command = new ListCompanyCommand();
        command.setCtx(this.createContext());

        List<Long> ids = new ArrayList<Long>();
        ids.add(1L);
        ids.add(2L);
        command.setIds(ids);
        companyService.deleteBatchCompany(command);
    }

    @Test
    @Rollback(false)
    public void testUpdate() {
        CompanyCommand command = new CompanyCommand();
        command.setCtx(this.createContext());
        CompanyVo vo = new CompanyVo();
        vo.setId(1L);
        command.setVo(vo);
        companyService.updateCompany(command);
    }

    @Test
    @Rollback(false)
    public void testUpdateBatch() {
        CompanyBatchCommand command = new CompanyBatchCommand();
        command.setCtx(this.createContext());
        CompanyVo v1 = new CompanyVo();
        v1.setId(1L);
        CompanyVo v2 = new CompanyVo();
        v2.setId(2L);
        List<CompanyVo> list = new ArrayList<CompanyVo>();
        list.add(v1);
        list.add(v2);
        command.setVos(list);

        companyService.updateBatchCompany(command);
    }

    @Test
    public void testGet() {
        GetCompanyCommand command = new GetCompanyCommand();
        command.setCtx(this.createContext());
        command.setId(1L);
        CompanyVo vo = companyService.getCompany(command);
        System.out.println("vo：" + vo);
    }

    @Test
    public void testQueryPage() {
        CompanyQueryCommand command = new CompanyQueryCommand();
        command.setCtx(this.createContext());
        PageVo pageVo = new PageVo();
        pageVo.setFieldName("id");
        pageVo.setPageNo(1);
        pageVo.setPageSize(8);
        pageVo.setDirection(Sort.Direction.DESC.name());
        command.setPageVo(pageVo);
        CompanyVo vo = new CompanyVo();
        command.setVo(vo);

        Page<CompanyVo> page = companyService.queryCompanyPage(command);
        System.out.println("page:" + page);
        if (page == null) return;
        for (CompanyVo v : page.getContent()) {
            System.out.println("vo:" + v);
        }
    }

    @Test
    public void testQueryList() {
        CompanyQueryCommand command = new CompanyQueryCommand();
        command.setCtx(this.createContext());
        CompanyVo vo = new CompanyVo();
        command.setVo(vo);

        List<CompanyVo> list = companyService.queryCompanyList(command);
        for (CompanyVo v : list) {
            System.out.println("vo:" + v);
        }
    }

}
